
package model.math;

import java.util.Random;

import model.info.EquipmentList;
import bean.CharacterBEAN;
import bean.EquipmentBEAN;
import dao.EquipmentDAO;

public class Murder {
	private int	attackerHit;
	private int	attackerAvoidance;
	private int	defenderHit;
	private int	defenderAvoidance;
	private int	rounds;
	private int	bulletVoordeel;

	public Murder( CharacterBEAN attacker, CharacterBEAN defender,
			int attackWeapon, int bullets, EquipmentDAO equipmentDAO ) {
		attackerHit = ( 35 * ( attacker.getShootExperience() / 100 ) )
				+ ( getWeaponpowerById( attackWeapon ) / 100 );
		defenderHit = ( 35 * ( defender.getShootExperience() / 100 ) )
				+ ( getDefenceWeapon( defender, equipmentDAO ) / 100 );
		initializeBullets( attacker, defender, equipmentDAO, bullets );
		attackerAvoidance = 35 + attacker.getLevel()
				+ getGuards( attacker, equipmentDAO ) + vipBonus( attacker );
		defenderAvoidance = ( 35 * bulletVoordeel ) + defender.getLevel()
				+ getGuards( defender, equipmentDAO ) + vipBonus( defender );
		rounds = ( bullets / defender.getLevel() );
		printStats();
		killJob( attacker, defender );
	}
	
	public String killJob( CharacterBEAN attacker, CharacterBEAN defender ) {
		System.out.println( "game started..." );
		while( rounds >= 0 || attacker.getHealth() <= 0 || defender.getHealth() >= 0 ){
			//Attackers turn
			if( attackerHit < randomInt( 100 ) && defenderAvoidance > randomInt( 100 ) ){
				defender.setHealth( defender.getHealth() - 1 );
			}
			//Defenders turn
			if( defenderHit < randomInt( 100 ) && attackerAvoidance > randomInt( 100 ) ){
				attacker.setHealth( attacker.getHealth() - 1 );
			}
			rounds--;
		}
		return finish( attacker, defender );
	}

	private String finish( CharacterBEAN attacker, CharacterBEAN defender ) {
		//TODO how do I fix this?!?
		return "attacker:health=" + attacker.getHealth() + ", defender:health=" + defender.getHealth();
	}

	private int randomInt( int i ) {
		Random random = new Random();
		return random.nextInt( i );
	}

	private int getWeaponpowerById( int weaponId ) {
		for( EquipmentList el : EquipmentList.values() ) {
			if( weaponId == el.getId() ) {
				return el.getPower();
			}
		}
		return 0;
	}

	private int getDefenceWeapon( CharacterBEAN character,
			EquipmentDAO equipmentDAO ) {
		EquipmentBEAN equipment = equipmentDAO.getEquipmentByID( character.getCharacterID() );
		if( equipment.isUziSMG() ){
			return 500;
		} else if( equipment.isDesertEagle() ){
			return 350;
		} else if( equipment.isBarakSP21() ){
			return 200;
		} else if( equipment.isColtM1911() ){
			return 100;
		}
		return 500;
	}

	private int vipBonus( CharacterBEAN character ) {
		if( character.isVip() ) {
			return 3;
		} else {
			return 0;
		}
	}

	private int getGuards( CharacterBEAN character, EquipmentDAO equipmentDAO ) {
		EquipmentBEAN equipment = equipmentDAO.getEquipmentByID( character.getCharacterID() );
		return equipment.getBodyguard();
	}

	private void initializeBullets( CharacterBEAN attacker,
			CharacterBEAN defender, EquipmentDAO equipmentDAO, int bullets ) {
		EquipmentBEAN attackerEquipment = equipmentDAO.getEquipmentByID( attacker.getCharacterID() );
		EquipmentBEAN defenderEquipment = equipmentDAO.getEquipmentByID( defender.getCharacterID() );
		bulletVoordeel = bullets / getBullets( defender, defenderEquipment, bullets, equipmentDAO );
		attackerEquipment.setBullet( attackerEquipment.getBullet() - bullets );
		equipmentDAO.updateEquipment( attackerEquipment );
	}

	private int getBullets( CharacterBEAN defender,
			EquipmentBEAN defenderEquipment, int bullets, EquipmentDAO equipmentDAO ) {
		int defenderBullets = 0;
		if( ( defenderEquipment.getBullet() * ( defender.getBackfire() / 100) ) < bullets ){
			defenderBullets = defenderEquipment.getBullet();
			defenderEquipment.setBullet( 0 );
		} else {
			defenderBullets = bullets;
			defenderEquipment.setBullet( defenderEquipment.getBullet() - bullets );
		}
		equipmentDAO.updateEquipment( defenderEquipment );
		return defenderBullets;
	}
	
	private void printStats(){
		System.out.println("Hit attacker: " + attackerHit + ", defender: " + defenderHit );
		System.out.println("Avoidance attacker: " + attackerAvoidance + ", defender: " + defenderAvoidance );
		System.out.println("rounds: " + rounds );
		System.out.println("bulletVoordeel: " + bulletVoordeel);
	}
}
